package org.example.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/jump")
public class JumpServlet extends HttpServlet {

    private static final ObjectMapper M = new ObjectMapper();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //约定好前后端接口格式：响应{ok: boolean}
        Map<String, Object> map = new HashMap<>();
        //模拟用户登录验证账号密码:
        if("abc".equals(username) && "123".equals(password)){
            map.put("ok", true);//账号abc，密码123就允许登录
        }else{
            map.put("ok", false);//否则就不允许登录
        }
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");
        String json = M.writeValueAsString(map);
        resp.getWriter().write(json);
    }
}